package org.yzh.web.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yzh.protocol.commons.Bit;
import org.yzh.protocol.commons.transform.AttributeXyKey;
import org.yzh.protocol.commons.transform.attribute.Electric;
import org.yzh.protocol.t808.T0200;
import org.yzh.web.model.entity.Device;
import org.yzh.web.model.entity.DeviceDO;

import java.util.Date;
import java.time.ZoneId;

public class DeviceUtil {

    private final static Logger log = LoggerFactory.getLogger(DeviceUtil.class);

    public static Device deviceDO2Device(DeviceDO deviceDO){
        if(deviceDO == null) return null;
        Device rtn = new Device();
        rtn.setDeviceId(deviceDO.getDeviceId());
        rtn.setPlateNo(deviceDO.getPlateNo());
        rtn.setProtocolVersion(deviceDO.getProtocolVersion());
        rtn.setMobileNo(deviceDO.getMobileNo());
        rtn.setAgencyId(deviceDO.getAgencyId());
        rtn.setDriverId(deviceDO.getDriverId());
        if (deviceDO.getLocation() != null) {
            T0200 location = deviceDO.getLocation();
            rtn = getLocation(location, rtn);
        }
        return rtn;
    }

    public static Device getLocation(T0200 location, Device device){
        if (location.getDeviceTime() != null) {
            Date date = Date.from(location.getDeviceTime().atZone(ZoneId.of("Asia/Shanghai")).toInstant());
            device.setLocationTime(date);
        }
        device.setServerTime(new Date());
        device.setDirection(location.getDirection());
        device.setSos(Bit.get(location.getWarnBit(), 0));
        location.transform();
        device.setLat(location.getLat());
        device.setLng(location.getLng());
        device.setSpeedKph(location.getSpeedKph());
        device.setAltitude(location.getAltitude());
        device.setDirection(location.getDirection());
        // 判断是否sos报警
        if (Bit.isTrue(location.getWarnBit(), 0)) {
            device.setSosTime(device.getLocationTime());
            device.setRemark(String.valueOf(location.getSerialNo()));
            log.warn("{} sos报警 {} 经度:{}, 纬度:{}", location.getClientId(), location.getDeviceTime()
                    , location.getLng(), location.getLat());
            //后续报警处理
        }
        if (location.getAttributes() != null) {
            Electric electric = (Electric) (location.getAttributes().get(AttributeXyKey.Electric));
            if (electric != null) {
                device.setElectricState(electric.getState());
                device.setElectricPercent(electric.getPercent());
            }
            int vol = location.getAttributeInt(AttributeXyKey.ElectricVoltage);
            if (vol != 0){
                device.setElectricVoltage(vol*0.1);
            }
        }
        return device;
    }

    /**
     * 判断是否sos报警
     * @param location
     * @return
     */
    public static Boolean isSOS(T0200 location){
        if (location == null) return null;
        int warnBit = location.getWarnBit();
        return Bit.isTrue(warnBit, 0);
    }

    /**
     * 判断是否电量低
     * @param location
     * @return
     */
    public static Boolean isElectricLow(T0200 location){
        if (location == null) return null;
        int warnBit = location.getWarnBit();
        return Bit.isTrue(warnBit, 7);
    }
}
